<?php
/**
 * kit's documentation repository abstraction
 *
 * This file is part of Webappkit, a packages system for PHP web apps
 * @link http://webappkit.net
 * @package webappkit
 * @subpackage admin
 * @author J.Ducastel <nospam0@ducastel.name>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */
class WakDocStorage extends WakTool implements iWakStorage {

    public static function toolFactory($id,$cfg=array()) {
        return new WakDocStorage($id,$cfg);
    }

    public function has($id) {
        return false;
    }

    public function get($id) {
        // building folder instance
        $folder=$this->_getFolder();
        $path=$folder->path().'/'.$id;
        if (substr($id,-5)=='.html')
            return new WakHtmlDocPage($path);
        if (substr($id,-9)=='.rest.txt'
        or substr($id,-8)=='.rst.txt')
            return new WakRestDocPage($path);
        return false;
    }

    public function save(&$obj) {
        return false;
    }

    public function delete($id) {
        return false;
    }

    public function search($params) {
        return false;
    }

    public function getList() {

    }

    /**
     * get all stored items
     * @return array(iWakDocPage)
     */
    public function getAll() {
        // building folder instance
        $folder=$this->_getFolder();
        // looping folder
        $list=array();
        foreach ($folder as $file) {
            $path=$folder->realPath().'/'.$file;
            // echo "<p>".substr($file,-5).'</p>';
            if (substr($file,-5)=='.html')
                $list[]=new WakHtmlDocPage($path);
            if (substr($file,-9)=='.rest.txt'
            or substr($file,-8)=='.rst.txt')
                $list[]=new WakRestDocPage($path);
        }
        return $list;
    }

    public function getClass() {
        return false;
    }

    public function getIdMember() {
        return false;
    }

    public function setLocale($locale) {
        $this->locale=$locale;
    }

    protected function _init() {
        $this->path=Webappkit::getPathFromId($this->getKitId());
    }

    /**
     * @var return WakFolder
     */
    protected function _getFolder() {
        $folder=new WakFolder($this->path.'/docs/'.$this->locale);
        if ($folder->exists())
            return $folder;
        $folder=new WakFolder($this->path.'/docs');
        return $folder;
    }
}